package components

import java.io.Serializable
import modelApp.EditorPais
import modelApp.EditorVillano
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.DropDownChoice
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.apache.wicket.model.IModel
import org.apache.wicket.model.PropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XLink

abstract class EdicionForm<T> extends Form<T> implements Serializable {
	var protected fbpanel= new FeedbackPanel("errores")
	
	new(String id, IModel<T> model) {
		super(id, model)
	}
}


class VillanoForm extends EdicionForm<EditorVillano> {
	
		extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	
	new(String id, IModel<EditorVillano> model) {
		super(id, model)
		addChild(new TextField<String>("nombre"))
		agregarSexo
		agregarSeñas
		agregarHobbies
		addChild(button("aceptar") [ onClick = [|
			this.modelObject.validate(fbpanel)
			this.modelObject.save
		] ])
		addChild(new FeedbackPanel("errores"))
	}
	
	def  agregarSeñas(Form<EditorVillano> it){
		addChild(new ListViewAndButton("senhas", modelObject.señasParticulares))
		addChild(new TextField<String>("nuevaSeña") )
		addChild(new XLink("addSeña") => [ onClick = [| this.modelObject.agregarSeña]])
	}
	
	def agregarHobbies(Form<EditorVillano> it){
		addChild(new ListViewAndButton("hobbies", modelObject.hobbies))
		addChild(new TextField<String>("nuevoHobbie"))
		addChild(new XLink("addHobbie") => [ onClick = [| this.modelObject.agregarHobbie] ])
			
	}
	
	
	
	def agregarSexo(Form<EditorVillano> it) {
		addChild(new Label("sexo"))
		addChild(
			new DropDownChoice<EditorVillano>("sexoSeleccionado") => [
				choices = new PropertyModel(this.modelObject, "sexosPosibles")
				choiceRenderer = choiceRenderer([m|m])
				required = true
				nullValid = true
			])
		addChild(button("agregarSexo") [onClick = [|this.modelObject.addSexo] ])
	}
	

	
}

class PaisForm extends EdicionForm<EditorPais> {
	
			extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods	
	
	new(String id, IModel<EditorPais> model) {
		super(id, model)
		addChild(new TextField<String>("nombre"))
		agregarCaracteristicas
		agregarConexiones
		agregarLugares
		addChild(fbpanel)
		addChild(button("aceptar") [ onClick = [|
			this.modelObject.validate(fbpanel)
			this.modelObject.save
		] ])
	}
	
	def agregarLugares(Form<EditorPais> it){
		addChild(new ListViewAndButton("lugaresInteresantes", modelObject.lugaresInteresantes))
		addChild(
			new DropDownChoice<EditorVillano>("lugarSeleccionado") => [
				choices = new PropertyModel(this.modelObject, "opcionesLugares")
				choiceRenderer = choiceRenderer([m|m])
				required = true
				nullValid = true
			])
		addChild(button("agregarLugar") [onClick = [|this.modelObject.agregarLugar] ])
	}
	
	def agregarConexiones(Form<EditorPais> it){
		addChild(new ListViewAndButton("conexiones", modelObject.conexiones))
		addChild(
			new DropDownChoice<EditorVillano>("paisSeleccionado") => [
				choices = new PropertyModel(this.modelObject, "paisesPosibles")
				choiceRenderer = choiceRenderer([m|m])
				required = true
				nullValid = true
			])
		addChild(button("agregarConexion") [onClick = [|this.modelObject.agregarConexion] ])
	}
	
	def agregarCaracteristicas(Form<EditorPais> it){
		addChild(new ListViewAndButton("caracteristicas", modelObject.caracteristicas))
		addChild(new TextField<String>("nuevaCaracteristica"))
		addChild(new XLink("addCaracteristica") => [ onClick = [| this.modelObject.agregarCaracteristica]])
	}
	
}